package com.wn.procurement.web;

import com.bestvike.linq.Linq;
import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.Material;
import com.wn.entity.Procurement;
import com.wn.entity.ShoppingList;
import com.wn.procurement.service.MaterialService;
import com.wn.procurement.service.ProcurementService;
import com.wn.procurement.vo.MaterialVo;
import com.wn.procurement.vo.ProcurementVo;
import com.wn.utils.OssManagerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/procurement")
public class ProcurementController {

    @Autowired
    private ProcurementService procurementService;
    @Autowired
    private ExcelToList excelToList;

    @Autowired
    private MaterialService  materialService;


    @RequestMapping("/upload")
    public ResponseEntity upload(MultipartFile file) {
        String url = null;
        try {
            String filename = file.getOriginalFilename();
            filename= UUID.randomUUID().toString().replace("-","")+"."+filename;
            url = OssManagerUtil.uploadImage(filename,file.getInputStream().available(),file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("400","fail","文件上传失败");
        }

        return new ResponseEntity(url);
    }


    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody @Validated Procurement procurement){
        procurementService.insert(procurement);
        return ResponseEntity.success;
    }

    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody @Validated Procurement procurement){
        procurementService.update(procurement);
        return ResponseEntity.success;
    }

    @RequestMapping("/query")
    public ResponseEntity query(@RequestBody @Validated ProcurementVo procurementVo){
        PageBean<Procurement> pageBean = procurementService.getByCondition(procurementVo);
        return new ResponseEntity(pageBean);
    }
    @RequestMapping("/getFileInfoById")
    public List<ShoppingList> getFileInfoById(@RequestBody @Validated Procurement procurement) throws Exception {
        List<ShoppingList> shoppingLists = excelToList.importExcelAction(procurement.getShoppingList());
        //List<ShoppingList> shoppingLists =new List<ShoppingList>();
        List<Material> MaterialLists= materialService.getMaterialBymaterialVo(new MaterialVo());
        return  null;
    }

    @RequestMapping("/Test")
    public String GetStr(  ) throws Exception {
        return  "null";
    }
    @RequestMapping("/TestAll")
    public  PageBean<Procurement> TestAll( ) throws Exception {
        PageBean<Procurement> byCondition = procurementService.getByCondition(new ProcurementVo());
        return byCondition;
    }

}
